import { createConnection } from "./dbutils.js";

const DEMAND_SQL_2 = "SELECT \
a.time, \
a.pue, \
a.all_dl, \
a.it_dl, \
a.kt_dl, \
a.qt_dl \
FROM \
cloud_computing_demo.pue_data a \
JOIN (SELECT time FROM cloud_computing_demo.pue_data ORDER BY time DESC LIMIT 5 ) b \
ON b.time = a.time \
ORDER BY  \
time ASC";

function queryBarData(success) {
    let connection = createConnection();
    connection.connect();
    connection.query(DEMAND_SQL_2, (err, res) => {
        if (err == null) {
            console.log(res);
            success(res);
        } else {
            throw new Error(err);
        }
    });
    connection.end();
}

const QUERY_DEVICE_LIST = 'SELECT \
a.device_id, \
a.device_name \
FROM \
cloud_computing_demo.device_config a';

function queryDeviceList(success) {
    let connection = createConnection();
    connection.connect();
    connection.query(QUERY_DEVICE_LIST, (err, res) => {
        if (err == null) {
            console.log(res);
            success(res);
        } else {
            throw new Error(err);
        }
    });
    connection.end();
}

const QUERY_DEVICE_METE_TABLE = "SELECT \
c.precinct_name, \
b.device_name, \
d.mete_name, \
a.mete_value, \
a.record_time, \
a.last_mete_value, \
CASE \
    WHEN a.last_mete_value = 0 \
    AND a.mete_value = 1 THEN \
        '开始运行' \
        WHEN a.last_mete_value = 1 \
        AND a.mete_value = 0 THEN \
            '停止运行' ELSE '状态改变' \
        END 'desc', \
e.dict_note  \
FROM \
usmschis.t_his_telesignaldata20230101 a \
JOIN usmsc.t_cfg_device b ON a.device_id = b.device_id \
JOIN usmsc.t_cfg_precinct c ON b.precinct_id = c.precinct_id \
JOIN usmsc.t_cfg_telesignal d ON a.device_id = d.device_id  \
AND a.mete_id = d.mete_id \
JOIN usmsc.t_cfg_dict e ON b.device_type = e.dict_code \
AND e.col_name = 'device_type' \
WHERE \
a.mete_kind = 0 \
AND a.mete_value <> a.last_mete_value \
ORDER BY \
record_time DESC \
LIMIT 200"

function queryDeviceTable(success) {
    let connection = createConnection();
    connection.connect();
    connection.query(QUERY_DEVICE_METE_TABLE, (err, res) => {
        if (err == null) {
            console.log(res);

            success(res);
        } else {
            throw new Error(err);
        }
    });
    connection.end();
}

const deviceIdList = [
    '00001006000000003431',
    '00001006000000003432',
    '00001006000000003444',
    '00001006000000003445'
]

const QUERY_DEVICE_LINE_LIST = 'SELECT \
b.device_name AS device_name, \
a.device_id AS device_id, \
a.mete_value AS mete_value, \
a.mete_id AS mete_id, \
c.mete_name AS mete_name, \
a.record_time AS record_time  \
FROM \
usmschis.t_his_telemeterdata20230101 a \
JOIN usmsc.t_cfg_device b ON a.device_id = b.device_id \
AND b.device_id IN (   "00001006000000003431", \
"00001006000000003432",\
"00001006000000003444", \
"00001006000000003445") \
JOIN usmsc.t_cfg_telemeter c ON c.mete_id = a.mete_id \
AND c.device_id = a.device_id \
WHERE \
c.mete_name = "正向有功电能" \
ORDER BY \
	device_id, \
	record_time ASC';


const QUERY_PUE_MONTH = 'SELECT \
a.time, \
a.pue \
FROM  \
cloud_computing_demo.pue_data a \
ORDER BY  \
time ASC'

function queryLineData(success) {
    let connection = createConnection();
    connection.connect();
    connection.query(QUERY_PUE_MONTH, (err, res) => {
        if (err == null) {
            console.log(res);
            success(res);
        } else {
            throw new Error(err);
        }
    });
    connection.end();
}

// const QUERY_USER_COUNT = 'SELECT COUNT(id) FROM loginuser';
const QUERY_USER_LIST = 'SELECT id,User,pwd FROM loginuser';

function queryUser(success) {
    // let query_count =  QUERY_USER_COUNT;
    let query_list = QUERY_USER_LIST;
    let connection = createConnection();
    connection.connect();

    connection.query(query_list, (err, res) => {
        if (err == null) {
               success(res);
        } else {
            throw new Error(err);
        }
        console.log(res);
    });
    connection.end();
}

export { queryBarData, queryDeviceList, queryDeviceTable, queryLineData, queryUser };